import java.net.*;
import java.io.*;

public class CheckMailServlet {

    public static void main(String s[]) {
	if (s.length != 3) {
	    System.out.println("=======================================");
	    System.out.println("CheckMail [mailServer:192.168.1.199] [user] [password]");
	    System.out.println("=======================================");
	    System.exit(0);
	}
   
	try {
	    CheckMail t = new CheckMail();
	    int i = t.checkMyMail(s[0], s[1], s[2]);
	    if (i==0) {
		System.out.println("No mail waiting.");
	    }
	    else {
		System.out.println
		    ("There " + (i==1?"is " :"are ") + i +
		     " message" +(i==1?"":"s")+ " waiting.");
	    }
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
    }

    private void send(BufferedWriter out, String s) throws IOException {
	out.write(s+"\n");
	out.flush();
    }

    private String receive(BufferedReader in) throws IOException {
	return in.readLine();
    }

    private int checkMyMail
	(String server, String user, String pass) throws IOException {
	Socket s = new Socket(server, 110);
	BufferedReader in  = new BufferedReader(new InputStreamReader(s.getInputStream()));
	BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

	receive(in);
	send(out, "USER " + user);
	receive(in);
	send(out, "PASS " + pass);
	receive(in);
	return GetNumberOfMessages(in, out);
    }

    public int GetNumberOfMessages
	(BufferedReader in, BufferedWriter out) throws IOException {
	int i = 0;
	String s;

	send(out, "LIST");
	receive(in);
	while((s = receive(in)) != null) {
	    if (!(s.equals("."))) {
		i++;
	    }
	    else
		return i;
	}
	return 0;
    }
}
